var CutScene = require('geoGameCutScene');
var TweenData = require('geoTweenData');
var Tween = require('geoTweenMoveTo');
var SqRunner = require('geoSequenceRunner');

cc.Class({
    extends: CutScene,

    properties: {
        waitDuration: {
            default: 3,
            type: cc.Float,
            tooltip: '在入场停留多久（大于0有效）'
        },
        tweenDatas: {
            default: [],
            type: [TweenData],
            tooltip: '过场结束渐变参考数据'
        }
    },

    start() {
        // 添加定时器
        this._enterGameFunc = this._enterGame.bind(this);
        if (this.waitDuration > 0)
            this.scheduleOnce(this._enterGameFunc, this.waitDuration);
        //
        this._configTween();
        // 处理点击结束
        this._configClickToEnterButton();
    },

    onClickToEnterGame(e) {
        e.target.getComponent(cc.Button).interactable = false;
        this._enterGame();
    },

    _enterGame() {
        // 取消定时器
        this.unschedule(this._enterGameFunc);
        //
        var sq = new SqRunner();
        var funcs = [];
        // 添加渐变
        this.tweenDatas.forEach(td => {
            td.tweenController = this._tween;
            funcs.push(
                td.play.bind(td, sq.next.bind(sq))
            );
        }, this);

        // 添加结束
        funcs.push(
            (() => {
                this.endCutScene();
            }).bind(this)
        );
        sq.run(funcs);
    },
    _configTween(){
        this._tween = this.getComponent(Tween);
        if (this._tween == null)
            this._tween = this.addComponent(Tween);
    },
    _configClickToEnterButton() {
        var clickEventHandler = new cc.Component.EventHandler();
        clickEventHandler.target = this.node; //这个 node 节点是你的事件处理代码组件所属的节点
        clickEventHandler.component = "geoTweenClickCutScene";//这个是代码文件名
        clickEventHandler.handler = "onClickToEnterGame";

        var button = this.getComponent(cc.Button);
        if (button == null)
            button = this.addComponent(cc.Button);
        //
        button.clickEvents.push(clickEventHandler);
    }

    // update (dt) {},
});